<?php

namespace common\models\spread;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%spread_activity}}".
 *
 * @property int $id
 * @property string $name 活动名称
 * @property string $thumb
 * @property string $description 说明
 * @property int $type_id 活动类型
 * @property int $begin_time 活动开始时间
 * @property int $end_time 活动结束时间
 * @property string $template 活动展示模板
 * @property int $recommend_at 推荐时间
 * @property int $status 活动状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class SpreadActivity extends \common\db\ActiveRecord {

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;
    //
    const TYPE_DEFAULT = 0;
    const TYPE_SHOP = 'shop';
    const TYPE_MATCH = 2;
    const TYPE_ENROLL = 3;
    const TYPE_LOTTERY = 4;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%spread_activity}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name'], 'required'],
            [['type_id', 'begin_time', 'end_time', 'status', 'created_at', 'recommend_at', 'updated_at'], 'integer'],
            [['name'], 'string', 'max' => 100],
            [['thumb', 'description'], 'string', 'max' => 255],
            [['template'], 'string', 'max' => 30],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '活动名称',
            'thumb' => 'Thumb',
            'description' => '说明',
            'type_id' => '活动类型',
            'begin_time' => '活动开始时间',
            'end_time' => '活动结束时间',
            'template' => '活动展示模板',
            'recommend_at' => '推荐时间',
            'status' => '活动状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }

    public function getType() {
        return $this->hasOne(SpreadType::className(), ['id' => 'type_id']);
    }

    public static function recommend($limit = 4) {
        return static::find()->active()->orderBy([
            'recommend_at' => SORT_DESC,
            'updated_at' => SORT_DESC,
        ])->limit($limit)->all();
    }

    public function getLink() {
        $name = 'default';
        $type = 301;
        $route = [
            'activity',
        ];
        if ($this->type_id && $this->type) {
            $name = $this->type->label;
            if ($name == static::TYPE_SHOP && !$this->template) {
                $type = 0;
            }
        }
        $route[] = $name;
        if ($this->template) {
            $route = [
                $this->template,
            ];
        }
        $route[] = $this->id;

        $url = implode('/', $route);
        if ($type) {
            $url = Url::h5($url);
        }

        return [
            'type' => $type,
            'name' => $name,
            'url' => $url,
        ];
    }

    public function fields() {
        return [
            'id',
            'name',
            'thumb' => function() {
                return Url::attach($this->thumb);
            },
            'description',
            'begin_time',
            'end_time',
            'template',
            'link',
        ];
    }

}
